home *** CD-ROM | disk | FTP | other *** search
- #include <suntool/sunview.h>
- #include <suntool/canvas.h>
- #include "defs.h"
-
- tatic short histicon[] =
- {
- #include "../images/icons/hist.icon"
- };
- EFINE_ICON_FROM_IMAGE(icon, histicon);
-
- #define HSIZE 128
- #define HEADER 10
- #define BOTTOM 30
- #define BOXSIZE 3
-
- Frame frame;
- anvas canvas;
- enu menu;
- ixwin *pw;
- har *filename;
- ouble lhist[90000];
- nt hist[90000], n = 0;
- nt greylevels;
- har *progname;
- nt max_val, min_val;
- nt winxsiz, winysiz;
- ixrect *pr, *dpr;
- nt vertical, logarithmic;
- nt maxh;
- ouble lmaxh;
-
- #ifdef STANDALONE
- ain(argc, argv, envp)
- #else
- ist_main(argc, argv, envp)
- #endif
- int argc;
- char **argv;
- char **envp;
- {
- register int i, j;
- int len, dummy;
- char *itoa();
- char message[BUFSIZ];
- char buf1[BUFSIZ];
- int c;
- colormap_t colormap;
-
- filename = strsave("stdin");
- vertical = FALSE;
- logarithmic = FALSE;
- progname = strsave(argv[0]);
- parse_profile(&argc, argv, envp);
-
- while ((gc = getopt(argc, argv, "W:lvb")) != EOF)
- switch (gc) {
- case 'l':
- logarithmic = TRUE;
- break;
- case 'v':
- vertical = TRUE;
- break;
- case 'b':
- bitrestrict = TRUE;
- break;
- case '?':
- errflag++;
- break;
- }
- if (errflag)
- error((char *) 0, "Usage: %s: [-l] [-v] [-b] [infile]\n", progname);
-
- for (stream = 0; optind < argc; stream++, optind++)
- if (stream == 0 && strcmp(argv[optind], "-") != 0) {
- filename = strsave(argv[optind]);
- if (freopen(argv[optind], mode[stream], f[stream]) == NULL)
- error("%s %s", PR_IO_ERR_INFILE, argv[optind]);
- }
-
- if ((pr = pr_load(stdin, &colormap)) == NULL)
- error(PR_IO_ERR_RASREAD);
-
- if (bitrestrict)
- greylevels = calc_max(pr)+1;
- else
- greylevels = MAXLEVEL(pr->pr_depth);
-
- statistics();
-
- calc_winsize();
-
- sprintf(message, "%s: %s", (logarithmic) ? "logarithmic histogram" : "histogram", filename);
- frame = window_create(NULL, FRAME,
- FRAME_LABEL, message,
- WIN_HEIGHT, winxsiz,
- WIN_WIDTH, winysiz,
- FRAME_ICON, &icon,
- FRAME_ARGS, argc, argv,
- 0);
- canvas = window_create(frame, CANVAS,
- CANVAS_HEIGHT, HSIZE + HEADER + BOTTOM,
- CANVAS_WIDTH, greylevels * BOXSIZE + 10,
- CANVAS_AUTO_SHRINK, FALSE,
- WIN_HORIZONTAL_SCROLLBAR, scrollbar_create(0),
- 0);
-
- dpr = mem_create(window_get(canvas, CANVAS_HEIGHT),
- window_get(canvas, CANVAS_WIDTH),
- 8);
-
- pw = canvas_pixwin(canvas);
-
- draw_histogram(pw);
-
- pw_vector(pw, 0, HSIZE + HEADER, greylevels * BOXSIZE, HSIZE + HEADER, PIX_SET, 1);
- for (i = 0; i < greylevels; i++) {
- len = (i % 100 == 0) ? 10 :
- (i % 50 == 0) ? 10 :
- (i % 10 == 0) ? 6 :
- (i % 2 == 0) ? 2 : 0;
- if (len)
- pw_vector(pw, i * BOXSIZE, HSIZE + HEADER, i * BOXSIZE, HSIZE + HEADER + len, PIX_SET, 1);
- if (i % 10 == 0)
- pw_text(pw, i * BOXSIZE, HSIZE + HEADER + 17, PIX_SRC, smallfont, itoa(i));
- }
-
- window_main_loop(frame);
-
- exit(0);
- }
-
- alc_winsize()
- {
- winxsiz = HSIZE + 28 + HEADER + BOTTOM;
- if ((greylevels * BOXSIZE + 10) > 500)
- winysiz = 500;
- else
- winysiz = greylevels * BOXSIZE + 10;
- }
-
- tatistics()
- {
- register int i, j;
- double sumsq, stddev, mean;
- int cumulate, count, val, sum, median, mode;
-
- sumsq = 0.;
- sum = 0;
- max_val = 0;
- min_val = 10000;
-
- for (j = 0; j < pr->pr_size.y; j++)
- for (i = 0; i < pr->pr_size.x; i++) {
- val = pr_get(pr, i, j);
- max_val = MAX(max_val, val);
- min_val = MIN(min_val, val);
- hist[val]++;
- sumsq += (val * val);
- sum += val;
- }
- n = pr->pr_size.y * pr->pr_size.x;
- mean = (double) sum / (double) n;
- stddev = sqrt(sumsq / (double) n - mean * mean);
-
- maxh = 0;
- for (i = 0; i < greylevels; i++)
- maxh = MAX(maxh, hist[i]);
-
- i = count = 0;
- while (count < n / 2)
- count += hist[i++];
- median = i - 1;
-
-
- i = mode = cumulate = 0;
-
- for (i = 0; i < greylevels; i++)
- if (hist[i] > cumulate) {
- cumulate = hist[i];
- mode = i;
- }
- fprintf(stderr, "mean = %3g\n", mean);
- fprintf(stderr, "median = %d\n", median);
- fprintf(stderr, "mode = %d\n", mode);
- fprintf(stderr, "std. dev. = %3g\n", stddev);
- fprintf(stderr, "max pixel = %d\n", max_val);
- fprintf(stderr, "min pixel = %d\n", min_val);
- }
-
- raw_histogram(pw)
- Pixwin *pw;
- {
- int temp, count, i, npts, dx, dy;
-
- npts = greylevels * 2 + 4;
- dx = 0;
- dy = HSIZE + HEADER;
-
- pw_batch_on(pw);
- pw_vector(pw, dx, dy, i * BOXSIZE, dy - hist[0] * HSIZE / maxh, PIX_SRC, 1);
-
- if (logarithmic)
- for (i = 0; i < greylevels; i++) {
- lhist[i] = log((double) hist[i] + 1.0); /* add 1 to avoid log(0) */
- if (lmaxh < lhist[i])
- lmaxh = lhist[i];
- }
-
- for (i = 0; i < greylevels; i++) {
- if (logarithmic)
- temp = (int) (lhist[i] * HSIZE / lmaxh);
- else
- temp = hist[i] * HSIZE / maxh;
- pw_vector(pw, i * BOXSIZE, dy - temp, (i + 1) * BOXSIZE, dy - temp, PIX_SRC, 1);
- if (vertical) {
- pw_vector(pw, i * BOXSIZE, dy - temp, i * BOXSIZE, dy, PIX_SET, 1);
- pw_vector(pw, (i + 1) * BOXSIZE, dy - temp, (i + 1) * BOXSIZE, dy, PIX_SET, 1);
- }
- }
- pw_batch_off(pw);
- }
-